package yns.springboot.guide.demo.data.db.jdbc.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import yns.springboot.guide.demo.data.db.jdbc.dao.UserDaoJDBC;
import yns.springboot.guide.demo.data.db.jdbc.dao.mapper.UserRowMapper;
import yns.springboot.guide.demo.entity.UserEntity;

@Repository
public class UserDaoJDBCImpl implements UserDaoJDBC {
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	public static String SELECT_SQL = "SELECT ID, Name, Sex,Age,Describe FROM T_USER";
	public static String SELECT_KEY = "SELECT (SELECT max(Id) FROM T_USER)+1";

	public List<UserEntity> getUser() {
		List<UserEntity> listUserEntities = jdbcTemplate.query(SELECT_SQL, new UserRowMapper());
		return listUserEntities;
	}

	public void addUser(UserEntity userEntity) {
		
		int ID = jdbcTemplate.queryForObject(SELECT_KEY, int.class);
		userEntity.setId(ID);
		
		String insert = "INSERT INTO T_USER(ID,Name,Sex,Age,Describe) VALUES(?,?,?,?,?)";
		jdbcTemplate.update(insert, userEntity.getId(),userEntity.getName(),userEntity.getSex(),userEntity.getAge(),userEntity.getDescribe());
	}


	public void deleteUser(int id) {
		String delete = "DELETE FROM T_USER WHERE id = ?";
		jdbcTemplate.update(delete, id);
		
	}
	
	public void updateUser(UserEntity userEntity) {
		String update = "UPDATE T_USER SET Describe= ? WHERE id = ?";
		jdbcTemplate.update(update, userEntity.getDescribe(),userEntity.getId());
	}

}



